import { createRouter, createWebHistory } from 'vue-router'
import { useStore } from '@/store'

const routes = [
    {
        path: '/',
        name: 'home',
        meta: {
            title: '海陵区凤栖园公墓',
            // requireAuth: true,
        },
        component: () => import('@/views/home/Home.vue')
    },
    {
        path: '/pedigreeOfAclan',
        name: 'pedigreeOfAclan',
        meta: {
            title: '传世族谱',
            requireAuth: true,
        },
        component: () => import('@/views/pedigreeOfAclan/PedigreeOfAclan.vue')
    },
    {
        path: '/createFamilyTree',
        name: 'createFamilyTree',
        meta: {
            title: '传世族谱',
            requireAuth: true,
        },
        component: () => import('@/views/pedigreeOfAclan/CreateFamilyTree.vue')
    },
    {
        path: '/relativeGraph',
        name: 'relativeGraph',
        meta: {
            title: '族谱关系图',
            requireAuth: true,
        },
        component: () => import('@/views/pedigreeOfAclan/RelativeGraph.vue')
    },
    {
        path: '/familyTree',
        name: 'familyTree',
        meta: {
            title: '传世族谱',
            requireAuth: true,
        },
        component: () => import('@/views/pedigreeOfAclan/FamilyTree.vue')
    },
    {
        path: '/top',
        name: 'top',
        meta: {
            title: '传世族谱排行榜',
            requireAuth: true,
        },
        component: () => import('@/views/pedigreeOfAclan/Top.vue')
    },
    {
        path: '/onlineMall',
        name: 'onlineMall',
        meta: {
            title: '在线商城',
            requireAuth: true,
        },
        component: () => import('@/views/onlineMall/OnlineMall.vue')
    },
    {
        path: '/commodityDetails',
        name: 'commodityDetails',
        meta: {
            title: '商品详情',
            requireAuth: true,
        },
        component: () => import('@/views/commodityDetails/CommodityDetails.vue')
    },
    {
        path: '/shoppingCart',
        name: 'shoppingCart',
        meta: {
            title: '购物车',
            requireAuth: true,
        },
        component: () => import('@/views/shoppingCart/ShoppingCart.vue')
    },
    {
        path: '/panhuaLease',
        name: 'panhuaLease',
        meta: {
            title: '盘花租借',
            requireAuth: true,
        },
        component: () => import('@/views/panhuaLease/PanhuaLease.vue')
    },
    {
        path: '/sweepGraveAppointment',
        name: 'sweepGraveAppointment',
        meta: {
            title: '预约祭扫',
            requireAuth: true,
        },
        component: () => import('@/views/sweepGraveAppointment/SweepGraveAppointment.vue')
    },
    {
        path: '/lookTombAppointment',
        name: 'lookTombAppointment',
        meta: {
            title: '预约看墓',
            requireAuth: true,
        },
        component: () => import('@/views/lookTombAppointment/LookTombAppointment.vue')
    },
    {
        path: '/satisfaction',
        name: 'satisfaction',
        meta: {
            title: '服务在线评价',
            requireAuth: true,
        },
        component: () => import('@/views/satisfaction/Satisfaction.vue')
    },
    {
        path: '/satisfactionSurvey',
        name: 'satisfactionSurvey',
        meta: {
            title: '服务在线评价',
            requireAuth: true,
        },
        component: () => import('@/views/satisfaction/SatisfactionSurvey.vue')
    },
    {
        path: '/buyGravesSearch',
        name: 'buyGravesSearch',
        meta: {
            title: '购墓查询',
            requireAuth: true,
        },
        component: () => import('@/views/buyGravesSearch/BuyGravesSearch.vue')
    },
    {
        path: '/tombRepair',
        name: 'tombRepair',
        meta: {
            title: '墓穴报修',
            requireAuth: true,
        },
        component: () => import('@/views/tombRepair/TombRepair.vue')
    },
    {
        path: '/repairRecord',
        name: 'repairRecord',
        meta: {
            title: '报修记录',
            requireAuth: true,
        },
        component: () => import('@/views/repairRecord/RepairRecord.vue')
    },
    {
        path: '/sacrificeAndSweepForGuests',
        name: 'sacrificeAndSweepForGuests',
        meta: {
            title: '代客祭扫',
            requireAuth: true,
        },
        component: () => import('@/views/sacrificeAndSweepForGuests/SacrificeAndSweepForGuests.vue')
    },
    {
        path: '/myOrders',
        name: 'myOrders',
        meta: {
            title: '我的订单',
            requireAuth: true,
        },
        component: () => import('@/views/myOrders/MyOrders.vue')
    },
    {
        path: '/renew',
        name: 'renew',
        meta: {
            title: '墓位缴费',
            requireAuth: true,
        },
        component: () => import('@/views/renew/Renew.vue')
    },


    {
        path: '/reservationService',
        name: 'reservationService',
        meta: {
            title: '预约服务',
            requireAuth: true,
        },
        component: () => import('@/views/reservationService/ReservationService.vue')
    },
    
    {
        path: '/cremationAppointment',
        name: 'cremationAppointment',
        meta: {
            title: '殡仪馆火化预约',
            requireAuth: true,
        },
        component: () => import('@/views/cremationAppointment/CremationAppointment.vue')
    },
    {
        path: '/appointmentRecord',
        name: 'appointmentRecord',
        meta: {
            title: '预约记录',
            requireAuth: true,
        },
        component: () => import('@/views/appointmentRecord/AppointmentRecord.vue')
    },
    {
        path: '/cloudSacrifice',
        name: 'cloudSacrifice',
        meta: {
            title: '云祭祀',
            // requireAuth: true,
        },
        component: () => import('@/views/cloudSacrifice/CloudSacrifice.vue')
    },
    {
        path: '/cloudSacrificeBuildLibrary',
        name: 'cloudSacrificeBuildLibrary',
        meta: {
            title: '建馆',
            // requireAuth: true,
        },
        component: () => import('@/views/cloudSacrifice/cloudSacrificeBuildLibrary/CloudSacrificeBuildLibrary.vue')
    },
    {
        path: '/cloudSacrificePersonalHall',
        name: 'cloudSacrificePersonalHall',
        meta: {
            title: '云祭祀',
            // requireAuth: true,
        },
        component: () => import('@/views/cloudSacrifice/cloudSacrificePersonalHall/CloudSacrificePersonalHall.vue')
    },
    {
        path: '/cloudCommemorate',
        name: 'cloudCommemorate',
        meta: {
            title: '实景云纪念'
        },
        component: () => import('@/views/cloudCommemorate/CloudCommemorate.vue')
    },
    {
        path: '/orderDetails',
        name: 'orderDetails',
        meta: {
            title: '订单详情'
        },
        component: () => import('@/views/orderDetails/OrderDetails.vue')
    },
]
console.log(import.meta.env.BASE_URL)
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    // history: createWebHistory('/'),
    // 路由跳转之后滚动条滚到顶部
    scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            // return { x: 0, y: 0 } vue2这种，vue3改成top
            return { top: 0 }
        }
    },
    routes
})

//路由判断登录 根据路由配置文件的参数
router.beforeEach((to, from, next) => {
    //改title
    if (to.meta.title) {
        document.title = to.meta.title as string
    }
    // next();
    console.log(to.matched)
    if (to.matched.some(record => record.meta.requireAuth)) { //判断该路由是否需要登录权限
        // 看后面授权能不能放这里，不要放home.vue页面
        console.log('需要登录');
        let store = useStore()
        console.log(store.openid)
        if (store.openid) { //判断当前的token是否存在 ； 登录存入的token
            next();
        } else {
            next({
                path: '/',
                query: {
                    redirect: to.fullPath
                } //将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next();
    }
    // if (to.matched.some(record => record.meta.requireAuth)) { //判断该路由是否需要登录权限
    //     console.log('需要登录');
    //     if (sessionStorage.token) { //判断当前的token是否存在 ； 登录存入的token
    //         next();
    //     } else {
    //         next({
    //             path: '/login',
    //             query: {
    //                 redirect: to.fullPath
    //             } //将跳转的路由path作为参数，登录成功后跳转到该路由
    //         })
    //     }
    // } else {
    //     next();
    // }
});

export default router